const app = getApp();
var utilMd5 = require('../util/md5.js');
Page({
	data: {
    bannerList: [{}],
    tempBannerList:[{}],
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navData: [],
    bkshopList:[],
    shopList:[],
    currentTab: 0,
    navScrollLeft: 0,
    page:1,
    //准备数据 
    menu: {
    },
    goodsList:[]
  },
	onLoad: function() {
    var that = this;
    that.getGoodsList();
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.log(res)
        if(res.data!=''){
            app.globalData.token=res.data
        }
        console.log("token=" + app.globalData.token)
      },
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }


    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })       
   
    
  },
  //获取商品列表
  getGoodsList(index) {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    //获取当前时间戳  
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var code = app.globalData.pdd_client_secret + 'client_id' + app.globalData.pdd_client_id + 'data_typeJSONopt_id0page1page_size100sort_type0timestamp' + timestamp + 'typepdd.ddk.goods.searchwith_coupontrue' + app.globalData.pdd_client_secret;
    // 22d0782469a7ed0bc2ab36d67687e3baffb7df06client_id239e2843addf47d28ef2c97e35724a20data_typeJSONopt_id0page1page_size10sort_type0timestamp1598510963typepdd.ddk.goods.searchwith_coupontrue22d0782469a7ed0bc2ab36d67687e3baffb7df06
    console.log(code)
    var MD5code = utilMd5.md5(code);   //code需要加密的数据
    wx.request({
      url: app.globalData.apiPdd + '',
      data: {
        page: 1,
        page_size: 100,
        sort_type:0,
        data_type: 'JSON',
        opt_id:0,
        client_id: '' + app.globalData.pdd_client_id,
        timestamp: '' + timestamp,
        with_coupon: true,
        type: 'pdd.ddk.goods.search',
        sign: '' + MD5code.toUpperCase()
      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        that.setData({
          shopList: that.data.shopList.concat(res.data.goods_search_response.goods_list)
        })
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  toTbkdetail:function(e){
    wx.navigateTo({
      url: '/pages/shop/pdd-item?id=' + e.currentTarget.dataset.id,
    })
  },
  pddtype:function(e){
    console.log(e)
    wx.navigateTo({
      url: 'pdd-baokuanlist?index='+e.currentTarget.dataset.index,
    })

  },
onShow(){

},
  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  switchTab(event) {
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth / 5;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
    setTimeout(function () {
      wx.stopPullDownRefresh(); //停止加载
      wx.hideNavigationBarLoading(); //隐藏加载icon
    }, 2000)
    this.setData({
      page: 1,
      shopList: []
    })
    that.getGoodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getCartList()
    var that = this;
    // that.setData({
    //   page: that.data.page + 1
    // })
    // that.getGoodsList();
  },

  
});
